package com.example.a1725185505.works.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.a1725185505.works.R;
import com.example.a1725185505.works.activity.UpdateuserActivity;
import com.example.a1725185505.works.base.BaseFragment;
import com.example.a1725185505.works.base.BaseListener;
import com.example.a1725185505.works.bean.UserByIdBean;
import com.example.a1725185505.works.model.UserByIdModel;
import com.example.a1725185505.works.util.SharedPreferencesUtils;

public class UserFragment extends BaseFragment {

    private TextView tv_user_mobile,tv_user_yonghuming;
    private TextView tv_user_xiugai;
    private SharedPreferencesUtils sharedPreferencesUtils;
    private Integer user_id;
    private UserByIdBean bean;

    @Override
    public int getLayoutFile() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView(View view) {
        tv_user_mobile = view.findViewById(R.id.tv_user_mobile);
        tv_user_yonghuming = view.findViewById(R.id.tv_username);
        tv_user_xiugai = view.findViewById(R.id.tv_user_xiugai);

        tv_user_xiugai.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        sharedPreferencesUtils = new SharedPreferencesUtils(getContext());
        user_id = Integer.parseInt(sharedPreferencesUtils.readString("userid"));

        UserByIdModel userModel = new UserByIdModel();
        userModel.getuserbyid(user_id , baseListener);
    }

    BaseListener<UserByIdBean> baseListener = new BaseListener<UserByIdBean>() {
        @Override
        public void onResponse(UserByIdBean userbyidBean) {
            if (userbyidBean != null){
                tv_user_yonghuming.setText(userbyidBean.getUsername());
                tv_user_mobile.setText(userbyidBean.getMobilenum());
            }else {
                showToast("kong");
            }
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_user_xiugai:
                Intent intent = new Intent(getActivity(),UpdateuserActivity.class);
                startActivity(intent);
                break;
        }
    }
}
